package com.zenchn.aliyun.oss.utils;


import com.alibaba.fastjson.JSON;

import java.util.*;

/**
 * com.zenchn.aliyun.oss 授权策略  组装类
 * @author dingxinfa
 * @date 2018-08-16
 *
 */
public class PolicyUtils {


    /***
     *  获取 sts 授权策略
     *  最终策略模板 实例
        {
         "Version": "1",
         "Statement":
            [
             {
                 "Effect": "Allow",
                 "Action": "com.zenchn.aliyun.oss:ListBuckets",
                 "Resource": "acs:com.zenchn.aliyun.oss:*:*:*"
             },
             {
             "Effect": "Allow",
             "Action":
                [
                 "com.zenchn.aliyun.oss:ListObjects",
                 "com.zenchn.aliyun.oss:GetBucketAcl"
                 ],
             "Resource":
                [
                 "acs:com.zenchn.aliyun.oss:*:*:zdzcbuilding",
                 "acs:com.zenchn.aliyun.oss:*:*:zdzc-temp"
                 ]
             },
                 {
                 "Effect": "Allow",
                 "Action":
                    [
                     "com.zenchn.aliyun.oss:GetObject",
                     "com.zenchn.aliyun.oss:DeleteObject",
                     "com.zenchn.aliyun.oss:ListParts",
                     "com.zenchn.aliyun.oss:AbortMultipartUpload",
                     "com.zenchn.aliyun.oss:PutObject",
                     "com.zenchn.aliyun.oss:GetObjectAcl"
                     ],
                 "Resource":
                    [
                     "acs:com.zenchn.aliyun.oss:*:*:zdzcbuilding/*",
                     "acs:com.zenchn.aliyun.oss:*:*:zdzc-temp/*"
                     ]
                 }
            ]
        }
     * @param bucketNameList
     * @return
     */

    public static String getStsPolicy(List<String> bucketNameList) {


        Map<String, Object> policy = new HashMap<>();
        policy.put("Version","1");

        List<Map<String, Object>> statementList = new ArrayList<Map<String, Object>>();

        Map<String, Object> statementOne=new HashMap<>(3);
        statementOne.put("Effect","Allow");
        statementOne.put("Action","com.zenchn.aliyun.oss:ListBuckets");
        statementOne.put("Resource","acs:com.zenchn.aliyun.oss:*:*:*");

        statementList.add(statementOne);


        Map<String, Object> statementTwo=new HashMap<>(3);
        statementTwo.put("Effect","Allow");

        List<String > actionTwoList=new ArrayList<>(Arrays.asList("com.zenchn.aliyun.oss:ListObjects", "com.zenchn.aliyun.oss:GetBucketAcl"));
        statementTwo.put("Action",actionTwoList);

        List<String > resourceTwoList=new ArrayList<>(bucketNameList.size());
        for (String bucketName:bucketNameList){
            resourceTwoList.add("acs:com.zenchn.aliyun.oss:*:*:"+bucketName);
        }
        statementTwo.put("Resource",resourceTwoList);

        statementList.add(statementTwo);


        Map<String, Object> statementThree=new HashMap<>(3);
        statementThree.put("Effect","Allow");

        List<String > actionThreeList=new ArrayList<>(
                Arrays.asList(
                    "com.zenchn.aliyun.oss:GetObject",
                    "com.zenchn.aliyun.oss:DeleteObject",
                    "com.zenchn.aliyun.oss:ListParts",
                    "com.zenchn.aliyun.oss:AbortMultipartUpload",
                    "com.zenchn.aliyun.oss:PutObject",
                    "com.zenchn.aliyun.oss:GetObjectAcl"
                )
        );

        statementThree.put("Action",actionThreeList);


        List<String > resourceThreeList=new ArrayList<>(bucketNameList.size());
        for (String bucketName:bucketNameList){
            resourceThreeList.add("acs:com.zenchn.aliyun.oss:*:*:"+bucketName+"/*");
        }
        statementThree.put("Resource",resourceThreeList);

        statementList.add(statementThree);

        policy.put("Statement",statementList);
        return   JSON.toJSONString(policy,true);
    }

    public static void main(String[] args){
        System.out.println(getStsPolicy(new ArrayList<>(Arrays.asList("zdzcbuilding", "zdzc-temp"))));
    }

}
